<?php

class ZapasModel extends NObject
{
	private $table = 'dbo.Zapas';
	private $tym_soutez = 'dbo.Tym_Soutez';
	private $rozhodci = 'dbo.Zapas_Rozhodci';
	private $zapas_hrac = 'dbo.Zapas_Hrac';
	private $connection;


	public static function initialize()
	{
		dibi::connect(NEnvironment::getConfig('database'));
	}
	
	public function __construct()
	{
		$this->connection = dibi::getConnection();
	}
	//-----------
	//FIND ALL
	//-----------	
	public function findAll()
	{
		return $this->connection->select('*')->from($this->table);
	}
	//-----------
	//FIND BY ID
	//-----------
	public function findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE id=%i",$id);
		//return $this->connection->select('*')->from($this->table)->where('id=%i',$id);
	}
	//-----------
	//FIND BY tym soutez id
	//-----------
	public function findByTymSoutezId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE tym_soutez_id_a=%i",$id, " OR tym_soutez_id_b=%i", $id);
		//return $this->connection->select('*')->from($this->table)->where('id=%i',$id);
	}
	//-----------
	//FIND BY tym soutez id
	//-----------
	public function findByZapasKolaId($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->table." WHERE zapas_kola_id=%i",$id);
	}
	//-----------
	//UPDATE
	//-----------
	public function update($id, array $data)
	{
		return $this->connection->update($this->table, $data)->where('id=%i', $id)->execute();
	}
	//-----------
	//INSERT
	//-----------	
	public function insert(array $data)
	{
		return $this->connection->insert($this->table, $data)->execute(dibi::IDENTIFIER);
	}
	//-----------
	//FIND ALL
	//-----------
	public function delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->table." WHERE id=%i", $id);
		//return $this->connection->delete($this->table)->where('id=%i', $id)->execute();
	}
	
	/* TYM_SOUTEZ */
	
	public function tym_soutez_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->tym_soutez." WHERE id=%i",$id);	
	}
	
	/* ROZHODCI */
	
	public function zapas_rozhodci_findAll()
	{
		return $this->connection->select('*')->from($this->rozhodci);
	}
	public function zapas_rozhodci_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->rozhodci." WHERE id=%i",$id);
	}
	public function zapas_rozhodci_delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->rozhodci." WHERE id=%i", $id);
	}
	public function zapas_rozhodci_update($id, array $data)
	{
		return $this->connection->update($this->rozhodci, $data)->where('id=%i', $id)->execute();
	}
	public function zapas_rozhodci_insert(array $data)
	{
		return $this->connection->insert($this->rozhodci, $data)->execute(dibi::IDENTIFIER);
	}

	/* HRACI */
	
	public function zapas_hrac_findAll()
	{
		return $this->connection->select('*')->from($this->zapas_hrac);
	}
	public function zapas_hrac_findById($id)
	{
		return $this->connection->query("SELECT [*] FROM ".$this->zapas_hrac." WHERE id=%i",$id);
	}
	public function zapas_hrac_delete($id)
	{
		return $this->connection->query("DELETE FROM ".$this->zapas_hrac." WHERE id=%i", $id);
	}
	public function zapas_hrac_update($id, array $data)
	{
		return $this->connection->update($this->zapas_hrac, $data)->where('id=%i', $id)->execute();
	}
	public function zapas_hrac_insert(array $data)
	{
		return $this->connection->insert($this->zapas_hrac, $data)->execute(dibi::IDENTIFIER);
	}
}
